package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDTO;
import com.qk.entity.LoginResultVo;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> pageUsers(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPage(), userDTO.getPageSize());
        List<User> users = userMapper.selectListUsers(userDTO);
        Page<User> pageInfo = (Page<User>) users;
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void saveUser(User user) {
        // 密码加密
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertUser(user);
    }

    @Override
    public void removeUser(List<Integer> ids) {
        userMapper.deleteUser(ids);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()+"123").getBytes()));
        userMapper.updateUser(user);
    }

    @Override
    public LoginResultVo login(String username, String password) {
        User user = userMapper.selectUserByUsername(username);
        String s = DigestUtils.md5DigestAsHex((password).getBytes());
        if (user == null || !user.getPassword().equals(s)){
            throw new BusinessException("用户名或密码错误");
        }

        if (user.getStatus() == 0){
            throw new BusinessException("用户被禁用");
        }

        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(user.getId());
        loginResultVo.setUsername(user.getUsername());
        loginResultVo.setName(user.getName());
        loginResultVo.setImage(user.getImage());
        loginResultVo.setRoleLabel(user.getRoleName());
        // 生成JWT令牌
        Map<String, Object> claims = Map.of("id", user.getId(), "username", user.getUsername());
        loginResultVo.setToken(JwtUtils.generateToken(claims));
        return loginResultVo;
    }

    @Override
    public List<User> listAllUsers() {
        return userMapper.selectListUsers(null);
    }

    @Override
    public List<User> listUsersByRole(String roleLabel) {
        return userMapper.listUsersByRole(roleLabel);
    }

    @Override
    public List<User> listUsersByDept(Integer deptId) {
        return userMapper.listUsersByDept(deptId);
    }
}